package com.shuang.graph17;
//
//import java.util.*;
//
////Bellman-ford算法 n个节点松弛n-1次
//public class Main {
//
//    // Define an inner class Edge
//    static class Edge {
//        int from;
//        int to;
//        int val;
//        public Edge(int from, int to, int val) {
//            this.from = from;
//            this.to = to;
//            this.val = val;
//        }
//    }
//
//    public static void main(String[] args) {
//        // Input processing
//        Scanner sc = new Scanner(System.in);
//        int n = sc.nextInt();
//        int m = sc.nextInt();
//        List<Edge> edges = new ArrayList<>();
//
//        for (int i = 0; i < m; i++) {
//            int from = sc.nextInt();
//            int to = sc.nextInt();
//            int val = sc.nextInt();
//            edges.add(new Edge(from, to, val));
//        }
//
//        // Represents the minimum distance from the current node to the original node
//        int[] minDist = new int[n + 1];
//
//        // Initialize the minDist array
//        Arrays.fill(minDist, Integer.MAX_VALUE);
//        minDist[1] = 0;
//
//        // Starts the loop to relax all edges n - 1 times to update minDist array
//        for (int i = 1; i < n; i++) {
//
//            for (Edge edge : edges) {
//                // Updates the minDist array
//                if (minDist[edge.from] != Integer.MAX_VALUE && (minDist[edge.from] + edge.val) < minDist[edge.to]) {
//                    minDist[edge.to] = minDist[edge.from] + edge.val;
//                }
//            }
//        }
//
//        // Outcome printing
//        if (minDist[n] == Integer.MAX_VALUE) {
//            System.out.println("unconnected");
//        } else {
//            System.out.println(minDist[n]);
//        }
//    }
//}


import java.util.*;

public class Main {

    // Define an inner class Edge
    static class Edge {
        int from;
        int to;
        int val;
        public Edge(int from, int to, int val) {
            this.from = from;
            this.to = to;
            this.val = val;
        }
    }

    public static void main(String[] args) {
        // Input processing
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        List<List<Edge>> graph = new ArrayList<>();

        for (int i = 0; i <= n; i++) {
            graph.add(new ArrayList<>());
        }

        for (int i = 0; i < m; i++) {
            int from = sc.nextInt();
            int to = sc.nextInt();
            int val = sc.nextInt();
            graph.get(from).add(new Edge(from, to, val));
        }

        // Declare the minDist array to record the minimum distance form current node to the original node
        int[] minDist = new int[n + 1];
        Arrays.fill(minDist, Integer.MAX_VALUE);
        minDist[1] = 0;

        // Declare a queue to store the updated nodes instead of traversing all nodes each loop for more efficiency
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(1);

        // Declare a boolean array to record if the current node is in the queue to optimise the processing
        //节点在队列中标记一下 节点已经在队列中就不再重复加入了 重复加入也不影响结果但会对性能有影响
        boolean[] isInQueue = new boolean[n + 1];

        while (!queue.isEmpty()) {
            int curNode = queue.poll();
            isInQueue[curNode] = false; // Represents the current node is not in the queue after being polled
            for (Edge edge : graph.get(curNode)) {
                if (minDist[edge.to] > minDist[edge.from] + edge.val) { // Start relaxing the edge
                    minDist[edge.to] = minDist[edge.from] + edge.val;
                    if (!isInQueue[edge.to]) { // Don't add the node if it's already in the queue
                        queue.offer(edge.to);
                        isInQueue[edge.to] = true;
                    }
                }
            }
        }

        // Outcome printing
        if (minDist[n] == Integer.MAX_VALUE) {
            System.out.println("unconnected");
        } else {
            System.out.println(minDist[n]);
        }
    }
}